package handler

import (
	"encoding/xml"
	"fmt"
	"gitee.com/Cauchy_AQ/vxopen/api/internal/logic/wechat"
	"io"
	"net/http"

	"gitee.com/Cauchy_AQ/vxopen/api/internal/svc"
	"gitee.com/Cauchy_AQ/vxopen/api/internal/types"
	"github.com/zeromicro/go-zero/rest/httpx"
)

func eventHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.EventReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		// 手动解析 xml 参数，go-zero 不支持
		body, _ := io.ReadAll(r.Body)
		_ = xml.Unmarshal(body, &req)
		fmt.Println("上行的xml数据包：", string(body))

		l := wechat.NewEventLogic(r.Context(), svcCtx)
		resp, _ := l.Event(&req)
		w.Header().Set("Content-Type", "application/xml")
		w.Write([]byte(resp))
	}
}
